<template>
  <view class="order">
    <view class="flex-c tabs">
      <u-tabs
        :list="list2"
        lineWidth="50"
        lineColor="#138E57"
        @change="tabChange"
      ></u-tabs>
    </view>
	<view class="search flex-jb flex-ac">
	  <u-search
	    placeholder="搜索"
	    v-model="queryParams.orderCode"
	    :showAction="false"
	    bgColor="#fff"
		@search="search"
		:clearabled="true"
		 @clear="searchClear"
	  ></u-search>
	  <view style="margin-left: 30rpx" @click="show = true">
	    <u-icon name="list-dot" size="20"></u-icon>
	  </view>
	</view>

    <view class="order-list">
        <view
          v-for="item of listData"
          :key="item.id"
          class="item-info">
		  <view class="" @click=" $route({ url: '/pages/orderDetails/index', params: { id: item.id, }, }) ">
		  	
		  
			  <view class="item-title flex-ac flex-jb l-border-bottom">
				<view>订单号：{{ item.orderCode }}</view>
				<view>{{ dict.data.order_status[item.orderStatus] }}</view>
			  </view>
			  <view class="form l-border-bottom">
				<view class="form-type">{{
				  item.serviceType == 1 ? "油脂收运" : "油脂清掏"
				}}</view>
				<view class="form-item" v-if="item.oilName">
				  <view>收运油品：</view>
				  <view>{{ item.oilName }}</view>
				</view>
				<view class="form-item">
				  <view>申请人：</view>
				  <view>{{ userInfo.nickName }}</view>
				</view>
				<view class="form-item">
				  <view>申请时间：</view>
				  <view>{{item.createTime}}</view>
				</view>            
				<view class="form-item" v-if="item.recyclingCompanyName">
				  <view>收运单位：</view>
				  <view>{{ item.recyclingCompanyName }}</view>
				</view>
				<view class="form-item" v-if="item.driverName">
				  <view>收运人姓名：</view>
				  <view>{{ item.driverName }}</view>
				</view>
			  </view>
			</view>
          <view class="flex-je">
            <template v-if="item.orderStatus == 0">
              <view class="button-light" @click="cancelOrder(item)">
                取消订单
              </view>
            </template> 
            <template v-if="item.orderStatus == 3">
              <view class="button-deep" @click="reAppointment(item)">
                重新申请
              </view>
            </template>
          </view>
        </view>
    </view>
  
	<u-popup :show="show">
	  <view class="popup">
	    <view class="time-form l-border-bottom" @click="calendarShow = true">
	      <view class="label">日期选择</view>
	      <view class="time flex-c">
	        <view>{{ startTime ? startTime : "起始日期" }} </view>
	        <view>-</view>
	        <view>{{ endTime ? endTime : "结束日期" }} </view>
	      </view>
	    </view>
	    <view class="type-form">
	      <view class="label">类型选择</view>
	      <view class="type flex-jc">
	        <LSelect
			style="width: 100%;"
	          placeholder="选择类型"
	          v-model="queryParams.serviceType"
	          border="none"
	          :localdata="dict.option.emission_unit_service_type"
	        ></LSelect>
	      </view>
	    </view>
	    <view class="submit">
	      <view class="close" @click="cancleSearch">取消</view>
	      <view class="confirm" @click="submitSearch">确定</view>
	    </view>
	  </view>
	</u-popup>
	<u-calendar
	  :show="calendarShow"
	  mode="range"
	  :defaultDate="calendar.defaultDate"
	:maxDate="calendar.maxDate"
	:minDate="calendar.minDate"
	 :monthNum="calendar.monthNum"
	  @confirm="confirmCalendar"
	  @close="calendarShow = false"
	></u-calendar>
	
	<uni-load-more :status="status_load"></uni-load-more>
  </view>
</template>

<script>
import Tabbar from "@/components/Tabbar/index.vue";
import LSelect from "@/components/LSelect";
import MescrollMixin from "@/components/Mescroll/mixin";
import {  
  receiptShipmentRecords,
  cancelApplication
} from "@/api";
import { getDicts } from "@/utils";
export default {
  mixins: [MescrollMixin],
  components: {
	LSelect,
  },
  data() {
    return {
		show: false,
		endTime: null,
		startTime: null,
		calendarShow: false,
		type: null,
		list2: [
			{
			  name: "全部",
			  value: "",
			},
			{
			  name: "待派单",
			  value: 0,
			},
			{
			  name: "收运中",
			  value: 4,
			},
			{
			  name: "已完成",
			  value: 2,
			},
			{
			  name: "已取消",
			  value: 3,
			},
		  ],
		dict: {},
		orderStatus: '',
		
		status_load: 'more',
		queryParams:{
			pageNum: 1,
			pageSize: 10,
			startTime:'',
			endTime:'',
			serviceType:'', //任务类型
			orderCode:'', //搜索框
		},
		startTime:'',
		endTime:'',
		listData:[],
		
		
		calendar: {
			minDate: '',
			maxDate: '',
			defaultDate: '',
			monthNum: 13,
		  },
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.userInfo;
    },
  },
  onReachBottom() {
  	if (this.status_load == 'noMore') {
  		return false;
  	}
  	this.receiptShipmentRecords()
  },
  onLoad(options) {
	  this.chooseTimed()
  },
  onShow() {
	  this.queryParams.orderCode = '';
	  this.listData = []
	  this.queryParams.pageNum = 1
	  this.$store.dispatch("userInfo");		
		this.receiptShipmentRecords()
		getDicts(["emission_unit_service_type","order_status"]).then((data) => {
		  this.dict = data;
		});
  },
  methods: {
	// 设置默认日期，最大值为今天，最小值为去年今天
	chooseTimed() {
	  let date = new Date();
	  let year = date.getFullYear();
	  let month = date.getMonth() + 1;
	  let day = date.getDate();
	  if (month <= 9) {
		month = '0' + month;
	  }
	  if (day <= 9) {
		day = '0' + day;
	  }
	  let minyear = year - 1;
	  this.calendar.minDate = minyear + '-' + month + '-' + day;
	  this.calendar.maxDate = year + '-' + month + '-' + day;
	  this.calendar.defaultDate = year + '-' + month + '-' + day;
	  console.log(this.calendar.defaultDate);
	},
	search(){
		this.listData = []
		this.queryParams.pageNum = 1
		this.receiptShipmentRecords()
	},
	searchClear(){
		this.queryParams.orderCode = '';
		  this.listData = []
		  this.queryParams.pageNum = 1	
		this.receiptShipmentRecords()
	},
	tabChange(val) {
		this.listData = []
		this.queryParams.pageNum = 1
	  this.queryParams.orderStatus = val.value;
	  this.receiptShipmentRecords();
	},
	// 选择时间范围
	confirmCalendar(e){
		this.startTime = e[0]
		this.endTime = e[e.length - 1]
		this.queryParams.startTime = e[0] + " 00:00:00";
		this.queryParams.endTime = e[e.length - 1] + " 23:59:59";
		this.calendarShow = false
	},
	cancleSearch(){
		let queryParams = {
			pageNum: 1,
			pageSize: 10,
			startTime:'',
			endTime:'',
			serviceType:'', //任务类型
			orderCode:'', //搜索框
		};
		this.startTime = ''
		this.endTime = ''
		this.queryParams = queryParams
		this.listData = []
		this.receiptShipmentRecords();
		this.show = false
	},
	submitSearch(){
		this.listData = []
		this.queryParams.pageNum = 1;
		this.receiptShipmentRecords();
		this.show = false
	},
	// 获取列表
	receiptShipmentRecords(){
		console.log(this.queryParams);
		this.status_load = 'loading'
		 receiptShipmentRecords(this.queryParams).then(res => {
			 // this.listData = res.data.records
			 uni.hideLoading()
			 let data = res.data.records
			 if (data) {
			 	this.listData = this.listData.concat(data)
			 }
			 if (this.queryParams.pageSize == data.length) {
			 	this.queryParams.pageNum += 1;
			 	this.status_load = 'more'
			 } else {
			 	this.status_load = 'noMore'
			 }
		 }) 
	},
    
	cancelOrder(item) {
      this.$modal(`是否取消该订单？`, () => {
        cancelApplication(item.id).then((res) => {
          this.$toast(`取消成功`);
		  this.listData = []
		  this.queryParams.pageNum = 1
		  this.receiptShipmentRecords();
        });
      });
    },
    reAppointment(item) {
      this.$modal(`是否重新申请该订单？`, () => {
        // 收油
        if(item.serviceType == '1'){
        	this.$route({
        		url: "/pages/apply/index",
        		params: {
        			id:item.id
        		},
        	});
        }else{
        	this.$route({
        		url: "/pages/apply/clean",
        		params: {
        			id:item.id
        		},
        	});
        }
      });
    },
 
    

  },
};
</script>

<style lang="less" scoped>
	.search {
	  padding: 118rpx 30rpx 0 30rpx;
	}
.order {
	.popup {
	  padding: 40rpx 30rpx;
	  .submit {
	    padding: 80rpx 0;
	    display: flex;
	    justify-content: center;
	    align-items: center;
	    .close,
	    .confirm {
	      border-radius: 200rpx;
	      padding: 20rpx 75rpx;
	    }
	    .confirm {
	      color: #fff;
	      background: linear-gradient(83deg, #48b453 0%, #2c9f79 100%);
	    }
	    .close {
	      background: #f7f7f7;
	      border-radius: 200rpx;
	      color: #138e57;
	      border: 2rpx solid #138e57;
	      margin-right: 30rpx;
	    }
	  }
	  .time-form {
	    padding-bottom: 38rpx;
	    margin-bottom: 32rpx;
	    .time {
	      :nth-child(1),
	      :nth-child(3) {
	        flex: 1;
	        text-align: center;
	        background: #f7f7f7;
	        padding: 16rpx 0;
	        border-radius: 200rpx;
	      }
	      :nth-child(2) {
	        margin: 0 18rpx;
	      }
	    }
	  }
	  .type {
	    background: #f7f7f7;
	    border-radius: 200rpx;
	  }
	  .label {
	    font-size: 30rpx;
	    color: #000000;
	    margin-bottom: 30rpx;
	  }
	}
  .tabs {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: #fff;
    z-index: 99;
  }
  .order-list {
    padding: 30rpx;
  }
  .item-info {
    margin-bottom: 30px;
    padding: 20rpx 30rpx 30rpx 30rpx;
    background: #fff;
    border-radius: 12rpx;
    .button-light,
    .button-deep {
      border-radius: 200rpx;
      padding: 16rpx 26rpx;
      margin: 20rpx 0 0 20rpx;
    }
    .button-light {
      background: #f7f7f7;
      border: 2rpx solid #138e57;
      color: #138e57;
    }
    .button-deep {
      background: linear-gradient(83deg, #48b453 0%, #2c9f79 100%);
      color: #ffffff;
    }
    .item-title {
      padding-bottom: 20rpx;
      view:nth-child(1) {
        font-size: 30rpx;
        color: #000000;
      }
      view:nth-child(2) {
        font-size: 28rpx;
      }
    }

    .form {
      padding: 30rpx 0;
      .form-type {
        font-size: 32rpx;
        color: #000000;
      }
      .form-item {
        display: flex;
        font-size: 30rpx;
        padding-top: 16rpx;
        view:nth-child(2) {
          color: #000;
        }
      }
    }
  }
}
</style>
